<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 14:54
 */

namespace app\admin\controller;


use app\admin\service\ConfigGroupService;
use app\admin\service\ConfigService;
use think\App;
use think\Request;

class ConfigGroup extends AdminBase
{
    protected $configGroupService = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->configGroupService = new ConfigGroupService();
    }

    /**
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $list = $this->configGroupService->listOfConfigGroup();

        return view('config_group/index', compact('list'));
    }

    public function create()
    {
        return view();
    }

    public function save(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        // 接收数据
        $data = [
            'group_name'  => $request->post('group_name', "", "trim"),
            'group_title' => $request->post('group_title', "", "trim"),
        ];
        // 验证数据
        $result = $this->validate($data, "ConfigGroupValidate.add");
        if (true !== $result) {
            return writeJson(config('status.error'), $result);
        }

        try {
            $res = $this->configGroupService->addConfigGroup($data);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), '添加成功');
        }

        return writeJson(config('status.error'), '添加失败');
    }

    /**
     * 编辑配置组
     * @param $id
     * @return \think\response\Json
     */
    public function edit($id)
    {
        // 根据id查询获取数据
        try {
            $info = $this->configGroupService->getConfigGroupById($id);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($info) {
            return view('config_group/edit', compact('info'));
        }
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        $id           = $request->post('id', 0, 'intval');
        $group_name   = $request->post('group_name', "", "trim");
        $group_title  = $request->post('group_title', "", "trim");
        $validateData = [
            'id'          => $id,
            'group_name'  => $group_name,
            'group_title' => $group_title,
        ];

        $updateData = [
            'group_name'  => $group_name,
            'group_title' => $group_title,
        ];

        // 验证数据
        $result = $this->validate($validateData, "ConfigGroupValidate.edit");
        if (true !== $result) {
            return writeJson(config('status.error'), $result);
        }

        try {
            $res = $this->configGroupService->updateById($id, $updateData);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), '编辑成功');
        }

        return writeJson(config('status.error'), '编辑失败');
    }

    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $res = $this->configGroupService->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), '删除成功');
        }

        return writeJson(config('status.error'), '删除失败');
    }

    public function param($id)
    {
        try {
            $list = (new ConfigService())->getAllConfigs($id);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($list) {
            return view('config_group/param', compact('list'));
        }

        $this->error('该配置组无参数');
    }
}